package deployr.client.http;

import static deployr.client.http.Streams.*;
import static java.text.MessageFormat.*;

import java.io.*;
import java.net.*;

import deployr.client.api.ProjectApi;

public class ProjectHttpClient implements ProjectApi {

	@Override
	public String readChecksum(String path, String projectKey) {
		try {
			URL url = new URL(format("{0}/projects/{1}/checksum", path, projectKey));
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			connection.setUseCaches(false);
			connection.setDoInput(true);
			connection.setDoOutput(false);
			InputStream inputStream = connection.getInputStream();
			int status = connection.getResponseCode();
			if (404 == status) throw new RuntimeException("project " + projectKey + " not found");
			String checksum = slurp(inputStream);
			inputStream.close();
			return checksum;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public int createEvent(String path, String projectKey, String event) {
		try {
			StringBuilder data = new StringBuilder();
			data.append("event").append("=").append(URLEncoder.encode(event, "UTF-8")).append("&");
			
			URL url = new URL(format("{0}/projects/{1}/events", path, projectKey));
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setDoInput(true);
			connection.setDoOutput(true);
			
			OutputStream outputStream = connection.getOutputStream();
			outputStream.write(data.toString().getBytes());
			
			int status = connection.getResponseCode();
			if (404 == status) throw new RuntimeException("project " + projectKey + " not found");
			return status;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void updateChecksum(String path, String projectKey, String checksum) {
		try {
			StringBuilder data = new StringBuilder();
			data.append("checksum").append("=").append(URLEncoder.encode(checksum, "UTF-8")).append("&");
			
			URL url = new URL(format("{0}/projects/{1}/checksum", path, projectKey));
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setDoInput(true);
			connection.setDoOutput(true);
			
			OutputStream outputStream = connection.getOutputStream();
			outputStream.write(data.toString().getBytes());
			
			int status = connection.getResponseCode();
			if (404 == status) throw new RuntimeException("project " + projectKey + " not found");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public String createUpdate(String path, String projectKey) {
		try {
			StringBuilder data = new StringBuilder();
			
			URL url = new URL(format("{0}/projects/{1}/updates", path, projectKey));
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setDoInput(true);
			connection.setDoOutput(true);
			
			OutputStream outputStream = connection.getOutputStream();
			outputStream.write(data.toString().getBytes());
			
			InputStream inputStream = connection.getInputStream();
			String updateKey = slurp(inputStream);
			inputStream.close();
			int status = connection.getResponseCode();
			if (404 == status) throw new RuntimeException("project " + projectKey + " not found");
			return updateKey;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public int webapp(String path, String projectKey) {
		try {
			URL url = new URL(format("{0}/projects/{1}/webapp", path, projectKey));
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setDoInput(true);
			connection.setDoOutput(false);

			int status = connection.getResponseCode();
			if (404 == status) throw new RuntimeException("project " + projectKey + " not found");
			return status;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public int webapp(String path, String projectKey, String updateKey) {
		try {
			URL url = new URL(format("{0}/updates/{1}/webapp", path, updateKey));
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setDoInput(true);
			connection.setDoOutput(false);

			int status = connection.getResponseCode();
			if (404 == status) throw new RuntimeException("update " + updateKey + " not found");
			return status;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public int war(String path, String projectKey) {
		try {
			URL url = new URL(format("{0}/projects/{1}/war", path, projectKey));
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setDoInput(true);
			connection.setDoOutput(false);

			int status = connection.getResponseCode();
			if (404 == status) throw new RuntimeException("project " + projectKey + " not found");
			return status;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public int war(String path, String projectKey, String updateKey) {
		try {
			URL url = new URL(format("{0}/updates/{1}/war", path, updateKey));
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setDoInput(true);
			connection.setDoOutput(false);

			int status = connection.getResponseCode();
			if (404 == status) throw new RuntimeException("update " + projectKey + " not found");
			return status;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void create(String path, String projectKey) {
		try {
			StringBuilder data = new StringBuilder();
			data.append("appId").append("=").append(URLEncoder.encode(projectKey, "UTF-8")).append("&");
			
			URL url = new URL(format("{0}/projects", path));
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setDoInput(true);
			connection.setDoOutput(true);
			
			OutputStream outputStream = connection.getOutputStream();
			outputStream.write(data.toString().getBytes());
			
			int status = connection.getResponseCode();
			if (404 == status) throw new RuntimeException("project " + projectKey + " not found");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public int deleteUpdates(String path, String projectKey) {
		try {
			URL url = new URL(format("{0}/projects/{1}/updates", path, projectKey));
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("DELETE");
			connection.setUseCaches(false);
			connection.setDoInput(true);
			connection.setDoOutput(false);

			int status = connection.getResponseCode();
			if (404 == status) throw new RuntimeException("project " + projectKey + " not found");
			return status;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}